c# udp文件传输
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using CSharpWin;using System.Diagnostics;namespace ReceiveFileDemo{ public partial class Form1 : Form { #region Fields private UdpReceiveFile udpReceiveFile; #endregion #region Constructor public Form1() { InitializeComponent(); //linkLabel1.Click = delegate(object sender, EventArgs e) //{ // Process.Start("www.csharpwin.com"); //}; } #endregion #region udpReceiveFile Events private void FileReceiveCancel( object sender, FileReceiveEventArgs e) { string name = string.Empty; if (e.Tag != null) { foreach (TraFransfersFileControl control1 in panelSend.Controls) { RequestSendFileEventArgs rse = control1.Tag as RequestSendFileEventArgs; if (rse != null && rse.TraFransfersFileStart.MD5 == e.Tag.ToString()) { name = rse.TraFransfersFileStart.FileName; BeginInvoke(new MethodInvoker(delegate() { panelSend.Controls.Remove(control1); control1.Dispose(); })); break; } } } else { TraFransfersFileControl control = e.ReceiveFileManager.Tag as TraFransfersFileControl; name = e.ReceiveFileManager.Name; if (control != null) { BeginInvoke(new MethodInvoker(delegate() { panelSend.Controls.Remove(control); control.Dispose(); })); } else { foreach (TraFransfersFileControl control1 in panelSend.Controls) { ControlTag tag = control1.Tag as ControlTag; if (tag != null && tag.MD5 == e.ReceiveFileManager.MD5) { BeginInvoke(new MethodInvoker(delegate() { panelSend.Controls.Remove(control1); control1.Dispose(); })); break; } } } } AppendLog(string.Format( "对方取消发送文件文件 {0} 。", name), true); } private void FileReceiveComplete( object sender, FileReceiveEventArgs e) { TraFransfersFileControl control = e.ReceiveFileManager.Tag as TraFransfersFileControl; if (control != null) { BeginInvoke(new MethodInvoker(delegate() { panelSend.Controls.Remove(control); control.Dispose(); })); } else { foreach (TraFransfersFileControl control1 in panelSend.Controls) { ControlTag tag = control1.Tag as ControlTag; if (tag != null && tag.MD5 == e.ReceiveFileManager.MD5) { BeginInvoke(new MethodInvoker(delegate() { panelSend.Controls.Remove(control1); control1.Dispose(); })); break; } } } AppendLog(string.Format( "文件 {0} 接收完成,MD5 校验: {1}。", e.ReceiveFileManager.Name, e.ReceiveFileManager.Success), true); } private void FileReceiveBuffer( object sender, FileReceiveBufferEventArgs e) { TraFransfersFileControl control = e.ReceiveFileManager.Tag as TraFransfersFileControl; if (control != null) { BeginInvoke(new MethodInvoker(delegate() { control.TraFransfersSize = e.Size; })); } else { foreach (TraFransfersFileControl control1 in panelSend.Controls) { ControlTag tag = control1.Tag as ControlTag; if (tag != null && tag.MD5 == e.ReceiveFileManager.MD5) { e.ReceiveFileManager.Tag = control1; BeginInvoke(new MethodInvoker(delegate() { control1.TraFransfersSize = e.Size; })); break; } } } } private void RequestSendFile( object sender, RequestSendFileEventArgs e) { TraFransfersFileStart traFransfersFileStart = e.TraFransfersFileStart; TraFransfersFileControl control = new TraFransfersFileControl(); control.IsSend = false; control.FileName = traFransfersFileStart.FileName; control.FileSize = traFransfersFileStart.Length; control.Image = traFransfersFileStart.Image; control.Comment = "接收文件"; control.Tag = e; control.Dock = DockStyle.Top; control.LabelClick = new LabelClickEventHandler(control_LabelClick); Invoke(new MethodInvoker(delegate() { panelSend.Controls.Add(control); control.BringToFront(); })); AppendLog(string.Format( "请求发送文件 {0}。", traFransfersFileStart.FileName), true); } #endregion #region Control Events private void button1_Click(object sender, EventArgs e) { udpReceiveFile = new UdpReceiveFile( int.Parse(tbLocalPort.Text)); udpReceiveFile.RequestSendFile = new RequestSendFileEventHandler(RequestSendFile); udpReceiveFile.FileReceiveBuffer = new FileReceiveBufferEventHandler(FileReceiveBuffer); udpReceiveFile.FileReceiveComplete = new FileReceiveEventHandler(FileReceiveComplete); udpReceiveFile.FileReceiveCancel = new FileReceiveEventHandler(FileReceiveCancel); udpReceiveFile.Start(); AppendLog(string.Format( "开始侦听,端口:{0}", udpReceiveFile.Port), false); } private void control_LabelClick( object sender, LabelClickEventArgs e) { TraFransfersFileControl control = sender as TraFransfersFileControl; RequestSendFileEventArgs rse = null; bool receive = false; switch (e.Flag) { case 0: rse = control.Tag as RequestSendFileEventArgs; rse.Cancel = true; panelSend.Controls.Remove(control); control.Dispose(); AppendLog(string.Format( "拒绝接收文件 {0}。", rse.TraFransfersFileStart.FileName), false); receive = true; break; case 1: rse = control.Tag as RequestSendFileEventArgs; FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { rse.Path = fbd.SelectedPath; control.StartTime = DateTime.Now; AppendLog(string.Format( "同意接收文件 {0}。", rse.TraFransfersFileStart.FileName), false); receive = true; } break; case 2: rse = control.Tag as RequestSendFileEventArgs; control.StartTime = DateTime.Now; rse.Path = Application.StartupPath; AppendLog(string.Format( "同意接收文件 {0}。", rse.TraFransfersFileStart.FileName), false); receive = true; break; case 3: ControlTag tag = control.Tag as ControlTag; udpReceiveFile.CancelReceive(tag.MD5, tag.RemoteIP); panelSend.Controls.Remove(control); control.Dispose(); AppendLog(string.Format( "取消接收文件 {0}。", tag.FileName), false); break; } if (receive) { ControlTag tag = new ControlTag( rse.TraFransfersFileStart.MD5, rse.TraFransfersFileStart.FileName, rse.RemoteIP); control.Tag = tag; control.IsSend = true; control.SetTag(3); udpReceiveFile.AcceptReceive(rse); } } #endregion #region Help Methods private void AppendLog(string text, bool async) { if (async) { BeginInvoke(new MethodInvoker(delegate() { int index = listBox1.Items.Add(text); listBox1.SelectedIndex = index; })); } else { int index = listBox1.Items.Add(text); listBox1.SelectedIndex = index; } } #endregion #region Override Methods protected override void OnClosed(EventArgs e) { base.OnClosed(e); if (udpReceiveFile != null) { udpReceiveFile.Dispose(); } } #endregion }}
评论